package com.leased.controller;

import com.leased.enums.BusinessStatusEnum;
import com.leased.exception.YCException;
import com.leased.handler.UserLoginLimitHandler;
import com.leased.pojo.UserLoginLimit;
import com.leased.request.userloginlimit.UserLoginLimitRequest;
import com.leased.utils.APIResponse;
import com.leased.utils.YCSystemStatusEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created by shencx on 2017/7/31.
 */
@Controller
@RequestMapping(value = "/api/limit")
public class LoginLimitController {

    final Logger LOG = LoggerFactory.getLogger(LoginLimitController.class);

    @Resource
    private UserLoginLimitHandler userLoginLimitHandler;

    /**
     * 更新用户权限信息
     * @param request
     * @param userLoginLimit
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/update",method = RequestMethod.POST)
    public APIResponse update(HttpServletRequest request, UserLoginLimitRequest userLoginLimit){
        APIResponse apiResponse = new APIResponse();
        try {
            userLoginLimitHandler.modifyLoginLimit(userLoginLimit);
            apiResponse.setStatus(BusinessStatusEnum.SUCCESS.getCode());
            apiResponse.setMsg(BusinessStatusEnum.SUCCESS.getDesc());
        } catch (Throwable e) {
            LOG.error("更新用户权限信息发生异常", userLoginLimit);
            apiResponse.setStatus(YCSystemStatusEnum.SYSTEM_ERROR.getCode());
            apiResponse.setMsg(YCSystemStatusEnum.SYSTEM_ERROR.getDesc());
        }
        return apiResponse;
    }

    /**
     * 获取权限信息列表
     * @param request
     * @param userId
     * @param userType
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public APIResponse<List<UserLoginLimit>> list(HttpServletRequest request,Integer userId,Integer userType){
        APIResponse<List<UserLoginLimit>> apiResponse = new APIResponse<>();
        List<UserLoginLimit> list = null;
        try {
            list = userLoginLimitHandler.getLoginLimitList(userId,userType);
            apiResponse.setStatus(BusinessStatusEnum.SUCCESS.getCode());
            apiResponse.setMsg(BusinessStatusEnum.SUCCESS.getDesc());
            apiResponse.setData(list);
        } catch (Throwable e) {
            LOG.error("获取权限列表信息发生异常", userId,userType);
            apiResponse.setStatus(YCSystemStatusEnum.SYSTEM_ERROR.getCode());
            apiResponse.setMsg(YCSystemStatusEnum.SYSTEM_ERROR.getDesc());
        }
        return apiResponse;
    }
}
